/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.scriptmark.config;

import com.jspx.scriptmark.Configurable;
import com.jspx.scriptmark.ScriptmarkEnv;
import com.jspx.scriptmark.core.block.*;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.DateUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2008-11-14
 * Time: 11:43:55
 */

public class TemplateConfigurable implements Configurable, Cloneable
{
    private final static Map<String, MacroBlock> regMacro = new HashMap<String, MacroBlock>();
    private Map<String, String> tagMap = new HashMap<String, String>();
    private Map<String, Object> hashMap = new HashMap<String, Object>();
    private String[] autoImports = null;
    private String[] autoIncludes = null;
    private String[] staticModels = null;
    private String[] searchPath = null;
    private Map<String, Object> globalMap = new HashMap<String, Object>();
    final static private TemplateConfigurable  instance = new TemplateConfigurable();

    static public Configurable getInstance()
    {
        return instance;
    }

    public TemplateConfigurable()
    {
        ///////////
        hashMap.put(ScriptmarkEnv.NumberFormat, "####.##");
        hashMap.put(ScriptmarkEnv.DateFormat, DateUtil.DAY_FORMAT);
        hashMap.put(ScriptmarkEnv.DateTimeFormat, DateUtil.CURRENCY_ST_FORMAT);
        hashMap.put(ScriptmarkEnv.TimeFormat, "HH:mm");

        hashMap.put(ScriptmarkEnv.Template_update_delay, 360);
        hashMap.put(ScriptmarkEnv.Template_cache_size, 120);

        hashMap.put(ScriptmarkEnv.MacroCallTag, "@");
        hashMap.put(ScriptmarkEnv.Language, "JavaScript");
        hashMap.put(ScriptmarkEnv.Syncopate, "<>");
        hashMap.put(ScriptmarkEnv.VariableBegin, "${");
        hashMap.put(ScriptmarkEnv.VariableEnd, "}");
        hashMap.put(ScriptmarkEnv.escapeVariable, "\\");
        hashMap.put(ScriptmarkEnv.htmlExtType, true);
        //////////

        ////////////Tag配置 begin
        tagMap.put("#setting", SettingBlock.class.getName());
        tagMap.put("#assign", AssignBlock.class.getName());
        tagMap.put("#if", IfBlock.class.getName());
        tagMap.put("#list", ListBlock.class.getName());
        tagMap.put("#macro", MacroBlock.class.getName());
        tagMap.put("#switch", SwitchBlock.class.getName());
        tagMap.put("#include", IncludeBlock.class.getName());
        tagMap.put("#script", ScriptBlock.class.getName());
        tagMap.put("#compress", CompressBlock.class.getName());
        tagMap.put("#try", TryBlock.class.getName());
        tagMap.put("!--#", CommentBlock.class.getName());  //兼容HTML注释   <!--#注释说明#-->
        ////////////Tag配置 end

        ////////////////默认为当前系统默认目录
        hashMap.put(ScriptmarkEnv.BasePath, System.getProperty("user.dir"));

        if (instance != null)
        {
            autoImports = instance.getAutoImports();
            autoIncludes = instance.getAutoIncludes();
            staticModels = instance.getStaticModels();
        }
    }

    public static void regMacro(String name, MacroBlock macroBlock)
    {
         regMacro.put(name,macroBlock);
    }

    public static MacroBlock getMacro(String name)
    {
         return regMacro.get(name);
    }

    public String[] getSearchPath()
    {
        return searchPath;
    }

    public void setSearchPath(String[] searchPath)
    {
        this.searchPath = searchPath;
    }

    public String[] getAutoIncludes()
    {
        return autoIncludes;
    }

    public void addAutoIncludes(String file)
    {
        if (!ArrayUtil.inArray(autoIncludes, file, true))
        {
            autoIncludes = ArrayUtil.add(autoIncludes, file);
        }
    }

    public void setTag(String name, String className)
    {
        tagMap.put(name, className);
    }

    public String removeTag(String name)
    {
        return tagMap.remove(name);
    }

    public Map<String, String> getTagMap()
    {
        return tagMap;
    }

    public void put(String name, Object o)
    {
        hashMap.put(name, o);
    }

    public String getString(String name)
    {
        return (String)hashMap.get(name);
    }

    public boolean getBoolean(String name)
    {
        return (Boolean) hashMap.get(name);
    }

    public int getInt(String name)
    {
        Object o = hashMap.get(name);
        if (o == null) return 0;
        if (o instanceof Integer)
        {
            return (Integer) o;
        } else
        {
            return Integer.getInteger(o.toString(), 0);
        }
    }

    public long getLong(String name)
    {
        Object o = hashMap.get(name);
        if (o == null) return 0;
        if (o instanceof Integer)
        {
            return (Integer) o;
        } else
        {
            return Long.parseLong(o.toString());
        }
    }

    public Map<String, Object> getGlobalMap()
    {
        return globalMap;
    }

    public void setGlobalMap(Map<String, Object> globalMap)
    {
        this.globalMap = globalMap;
    }

    public void setTagMap(Map<String, String> tagMap)
    {
        this.tagMap = tagMap;
    }

    public void setAutoIncludes(String[] autoIncludes)
    {
        this.autoIncludes = autoIncludes;
    }

    public void setHashMap(Map<String, Object> hashMap)
    {
        this.hashMap = hashMap;
    }

    public String[] getStaticModels()
    {
        return staticModels;
    }

    public void setStaticModels(String[] staticModels)
    {
        this.staticModels = staticModels;
    }

    public void addStaticModels(String str)
    {
        if (!ArrayUtil.inArray(staticModels, str, true))
        {
            staticModels = ArrayUtil.add(staticModels, str);
        }
    }

    public String[] getAutoImports()
    {
        return autoImports;
    }

    public void addAutoImports(String str)
    {
        if (!ArrayUtil.inArray(autoImports, str, true))
        {
            autoImports = ArrayUtil.add(autoImports, str);
        }
    }

    public void setAutoImports(String[] autoImports)
    {
        this.autoImports = autoImports;
    }

    public Configurable copy()
    {
        TemplateConfigurable tc = new TemplateConfigurable();
        tc.setTagMap(new HashMap<String, String>(tagMap));
        tc.setHashMap(new HashMap<String, Object>(hashMap));
        tc.setAutoIncludes(autoIncludes);
        tc.setAutoImports(autoImports);
        tc.setGlobalMap(new HashMap<String, Object>(globalMap));
        tc.setStaticModels(staticModels);
        return tc;
    }
}